import time

from selenium import webdriver
from selenium.webdriver.common.by import By
# driver = webdriver.Chrome()
# driver.get("https://baidu.com")

# 1. 单个元素
# el = driver.find_element(By.CSS_SELECTOR, "input#kw")
# el.send_keys("admin")
# time.sleep(2)

# 2. 多个元素
# links = driver.find_elements(By.CSS_SELECTOR, ".title-content-title")
# print(links[0].text)
#
# # 3. 链式定位（减少 XPath 嵌套）
# price = driver.find_element(By.CSS_SELECTOR, ".item").find_element(By.CSS_SELECTOR, ".price")


# driver.quit()

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

# driver = webdriver.Chrome(service=Service(r"D:\drivers\chromedriver.exe"))
# driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.service import Service
import time

# 1. 启动浏览器
service = Service(r"D:\drivers\chromedriver.exe")   # 换成你的路径
options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)   # 跑完不自动关
driver = webdriver.Chrome(service=service, options=options)

# 2. 打开登录页
driver.get("http://localhost:8802/#/login")
WebDriverWait(driver, 10).until(
    EC.title_contains("欢迎登录-黑马头条自媒体管理系统")
)

# 3. 点击【登录】按钮
login_btn = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.XPATH, '//*[@id="app"]/div/div/form/div[4]/div/button'))
)
login_btn.click()

# 4. 点击【添加文字】（第 9 个 li）
add_li = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.XPATH,
        '//*[@id="app"]/div/div[2]/section/div/form/div[2]/div/div/div[1]/div[1]/li[9]'))
)
add_li.click()

# 5. 点击并输入内容到 textarea
textarea = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.XPATH,
        '//*[@id="app"]/div/div[2]/section/div/form/div[2]/div/div/div[2]/div/div[2]/form/div/textarea'))
)
textarea.click()
textarea.clear()
textarea.send_keys("1")

# 6. 点击【确定】
ok_btn = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.XPATH,
        '//*[@id="app"]/div/div[2]/section/div/form/div[2]/div/div/div[2]/div/div[3]/div/button[2]/span'))
)
ok_btn.click()

# 7. 后续操作或 driver.quit() 视需要而定